package cn.lsh.writable;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class NetPage implements WritableComparable<NetPage> {
	private String page;

	/**
	 * 该值有两种情况：
	 * 1、pageType=0，表示页面pr值，默认为1
	 * 2、pageType=1，表示投票的权重
	 */
	private double pr = 1.0;
     /**拥其他网站的超链接，需要投票的目标网站*/
	private String[] relationPage;
	/** 判断当前对象是否表示的当前page，用来区分map输出的两类数据*/
	private int pageType;

	public NetPage() {

	}

	public NetPage(int pageType) {
		this.pageType = pageType;
	}

	public double getPr() {
		return pr;
	}

	public void setPr(double pr) {
		this.pr = pr;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String[] getRelationPage() {
		return relationPage;
	}

	public void setRelationPage(String users) {
		this.relationPage = StringUtils.split(users, " ");
	}

	public void setRelationPage(String[] relationPage) {
		this.relationPage = relationPage;
	}

	public int getPageType() {
		return pageType;
	}

	public void setPageType(int pageType) {
		this.pageType = pageType;
	}

	@Override
	public int compareTo(NetPage o) {
		return 0;
	}

	@Override
	public void write(DataOutput dataOutput) throws IOException {
		dataOutput.writeUTF(this.page);
		dataOutput.writeDouble(pr);
		dataOutput.writeUTF(arrToStr());
		dataOutput.writeInt(this.pageType);
	}

	@Override
	public void readFields(DataInput dataInput) throws IOException {
		this.page = dataInput.readUTF();
		this.pr = dataInput.readDouble();
		setRelationPage(dataInput.readUTF());
		this.pageType = dataInput.readInt();
	}

	public String arrToStr() {
		StringBuffer sb = new StringBuffer();
		if (this.relationPage != null && this.relationPage.length > 0) {
			sb.append(this.relationPage[0]);
			for (int i = 1; i < this.relationPage.length; i++) {
				sb.append(" ").append(this.relationPage[i]);
			}
		}
		return sb.toString();
	}

}
